package com.team06.util;

import com.team06.pojo.Cfie;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CfieXmlParserUtil {
    private static final Logger LOGGER = Logger.getLogger(CfieXmlParserUtil.class.getName());

    public static Cfie parseXml(String xmlPath) {
        Cfie entity = new Cfie();
        try {
            File file = new File(xmlPath);
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(file);
            Element root = document.getRootElement();

            // 解析 META 节点
            Element metaElement = root.getChild("META");
            if (metaElement != null) {
                entity.setSender(getChildTextSafe(metaElement, "SNDR"));
                entity.setReceiver(getChildTextSafe(metaElement, "RCVR"));
                String seqnText = getChildTextSafe(metaElement, "SEQN");
                if (seqnText != null) {
                    entity.setSequence(Integer.parseInt(seqnText));
                }
                entity.setDtime(getChildTextSafe(metaElement, "DDTM"));
                entity.setMtype(getChildTextSafe(metaElement, "TYPE"));
                entity.setStype(getChildTextSafe(metaElement, "STYP"));
            }

            // 解析 CRFT 节点
            Element crftElement = root.getChild("CRFT");
            if (crftElement != null) {
                entity.setCrftCode(getChildTextSafe(crftElement, "CODE"));
                entity.setCrftCftp(getChildTextSafe(crftElement, "CFTP"));
                entity.setCrftAwcd(getChildTextSafe(crftElement, "AWCD"));
                String stnmText = getChildTextSafe(crftElement, "STNM");
                if (stnmText != null) {
                    entity.setCrftStnm(Integer.parseInt(stnmText));
                }
                String rstnText = getChildTextSafe(crftElement, "RSTN");
                if (rstnText != null && !rstnText.isEmpty()) {
                    entity.setCrftRstn(Integer.parseInt(rstnText));
                }
                entity.setCrftCfsa(getChildTextSafe(crftElement, "CFSA"));
                String ttwcText = getChildTextSafe(crftElement, "TTWC");
                if (ttwcText != null && !ttwcText.isEmpty()) {
                    entity.setCrftTtwc(Double.parseDouble(ttwcText));
                }
                String mxlwText = getChildTextSafe(crftElement, "MXLW");
                if (mxlwText != null && !mxlwText.isEmpty()) {
                    entity.setCrftMxlw(Double.parseDouble(mxlwText));
                }
                String rllwText = getChildTextSafe(crftElement, "RLLW");
                if (rllwText != null && !rllwText.isEmpty()) {
                    entity.setCrftRllw(Double.parseDouble(rllwText));
                }
            }

        } catch (JDOMException | IOException | NumberFormatException e) {
            LOGGER.log(Level.SEVERE, "Failed to parse XML file: " + xmlPath, e);
            return null;
        }
        return entity;
    }

    private static String getChildTextSafe(Element parent, String childName) {
        Element child = parent.getChild(childName);
        return child != null ? child.getText() : null;
    }
}